package com.atom.module.coroutine.ext
//
//    /**
//     * All rights Reserved, Designed By www.rongdasoft.com
//     * @version V1.0
//     * @Title: BaseContinuationImpl
//     * @Description:
//     * @author: wuyongzheng
//     * @date: 2022/1/10
//     * @Copyright: 2022/1/10 www.rongdasoft.com Inc. All rights reserved.
//     */
//
//    import java.io.Serializable
//    import kotlin.coroutines.*
//    import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED
//    import kotlin.jvm.internal.FunctionBase
//    import kotlin.jvm.internal.Reflection
// TODO
//    @SinceKotlin("1.3")
//    internal abstract class BaseContinuationImpl(
//        public val completion: Continuation<Any?>?
//    ) : Continuation<Any?>, CoroutineStackFrame, Serializable {
//        // This implementation is final. This fact is used to unroll resumeWith recursion.
// TODO
//        public final override fun resumeWith(result: Result<Any?>) {
//            var current = this // 配置参数
//            var param = result // 配置入参
//            while (true) { //开始 死循环
//                probeCoroutineResumed(current) // 去SOURCE.kt中寻找，这是个debug埋点
//                with(current) {
//                    val completion = completion!! // fail fast when trying to resume continuation without completion
//                    val outcome: Result<Any?> =
//                        try {
//                            // 开始执行
//                            val outcome = invokeSuspend(param) // 真正的执行 invokeSuspend 去SOURCE.kt229行执行
//                            if (outcome === COROUTINE_SUSPENDED) return // 如果是挂起 则返回
//                            Result.success(outcome) // 如果不是挂起 则返回成功
//                        } catch (exception: Throwable) {
//                            Result.failure(exception) // 出现异常返回失败
//                        }
//                    releaseIntercepted() // 类似埋点 // this state machine instance is terminating
//                    if (completion is BaseContinuationImpl) { // 判断 == BaseContinuationImpl
//                        current = completion // 切换为新的
//                        param = outcome  // 将出参设置下个函数的入参
//                    } else {
//                        completion.resumeWith(outcome) // 反之最后执行一次resumeWith 并返回
//                        return
//                    }
//                }
//            }
//        }
//
//        protected abstract fun invokeSuspend(result: Result<Any?>): Any?
//
//        protected open fun releaseIntercepted() {
//            // does nothing here, overridden in ContinuationImpl
//        }
//
//        public open fun create(completion: Continuation<*>): Continuation<Unit> {
//            throw UnsupportedOperationException("create(Continuation) has not been overridden")
//        }
//
//        public open fun create(value: Any?, completion: Continuation<*>): Continuation<Unit> {
//            throw UnsupportedOperationException("create(Any?;Continuation) has not been overridden")
//        }
//
//        public override fun toString(): String =
//            "Continuation at ${getStackTraceElement() ?: this::class.java.name}"
//
//        // --- CoroutineStackFrame implementation
//
//        public override val callerFrame: CoroutineStackFrame?
//            get() = completion as? CoroutineStackFrame
//
//        public override fun getStackTraceElement(): StackTraceElement? =
//            getStackTraceElementImpl()
//    }
//
//    @SinceKotlin("1.3")
//    // State machines for named restricted suspend functions extend from this class
//    internal abstract class RestrictedContinuationImpl(
//        completion: Continuation<Any?>?
//    ) : BaseContinuationImpl(completion) {
//        init {
//            completion?.let {
//                require(it.context === EmptyCoroutineContext) {
//                    "Coroutines with restricted suspension must have EmptyCoroutineContext"
//                }
//            }
//        }
//
//        public override val context: CoroutineContext
//            get() = EmptyCoroutineContext
//    }
//
//  TODO
//    @SinceKotlin("1.3")
//    // State machines for named suspend functions extend from this class
//    internal abstract class ContinuationImpl(
//        completion: Continuation<Any?>?,
//        private val _context: CoroutineContext?
//    ) : BaseContinuationImpl(completion) {
//        constructor(completion: Continuation<Any?>?) : this(completion, completion?.context)
//TODO
//        public override val context: CoroutineContext
//            get() = _context!!
//TODO
//        @Transient
//        private var intercepted: Continuation<Any?>? = null
//TODO
//        public fun intercepted(): Continuation<Any?> = // 初始化 intercepted 并返回一个非空的
//            intercepted  // 根据逻辑 intercepted == null
//                ?: (context[ContinuationInterceptor]?.interceptContinuation(this) ?: this)
//                    .also { intercepted = it }
//TODO
//        protected override fun releaseIntercepted() {
//            val intercepted = intercepted
//            if (intercepted != null && intercepted !== this) {
//                context[ContinuationInterceptor]!!.releaseInterceptedContinuation(intercepted)
//            }
//            this.intercepted = CompletedContinuation // just in case
//        }
//    }
//
//    internal object CompletedContinuation : Continuation<Any?> {
//        override val context: CoroutineContext
//            get() = error("This continuation is already complete")
//
//        override fun resumeWith(result: Result<Any?>) {
//            error("This continuation is already complete")
//        }
//
//        override fun toString(): String = "This continuation is already complete"
//    }
//
//    @SinceKotlin("1.3")
//    // To distinguish suspend function types from ordinary function types all suspend function types shall implement this interface
//    internal interface SuspendFunction
//
//    @SinceKotlin("1.3")
//    // Restricted suspension lambdas inherit from this class
//    internal abstract class RestrictedSuspendLambda(
//        public override val arity: Int,
//        completion: Continuation<Any?>?
//    ) : RestrictedContinuationImpl(completion), FunctionBase<Any?>, SuspendFunction {
//        constructor(arity: Int) : this(arity, null)
//
//        public override fun toString(): String =
//            if (completion == null)
//                Reflection.renderLambdaToString(this) // this is lambda
//            else
//                super.toString() // this is continuation
//    }
//
//    @SinceKotlin("1.3")
//    // Suspension lambdas inherit from this class
//    internal abstract class SuspendLambda(
//        public override val arity: Int,
//        completion: Continuation<Any?>?
//    ) : ContinuationImpl(completion), FunctionBase<Any?>, SuspendFunction {
//        constructor(arity: Int) : this(arity, null)
//
//        public override fun toString(): String =
//            if (completion == null)
//                Reflection.renderLambdaToString(this) // this is lambda
//            else
//                super.toString() // this is continuation
//    }